<!--
    -  Copyright (c) 2018-2025, haohanwork.com All rights reserved.
  -->
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page.sync="page"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @row-update="handleUpdate"
        @row-save="handleSave"
        @row-del="rowDel">

        <template slot="menuLeft">
          <el-button
            v-show="permissionList.addBtn"
            icon="el-icon-plus"
            type="primary"
            size="small"
            @click="jumpToAdd">新增竞品</el-button>
        </template>
        <template slot="menuLeft">
          <sales_report_import ref="salesReportImport" style="display: inline"/>
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button
            v-show="permissionList.delBtn"
            icon="el-icon-delete"
            type="text"
            size="small"
            plain
            @click="rowDel(scope.row)">删除</el-button>
          <el-button
            v-show="permissionList.editBtn"
            icon="el-icon-view"
            type="text"
            size="small"
            plain
            @click="jumpToDetail(scope.row)">详情</el-button>
          <el-button
          v-show="scope.row.reportStatus !=='1'"
            icon="el-icon-edit-outline"
            type="text"
            size="small"
            plain
            @click="jumpToEdit(scope.row)">编辑</el-button>
          <el-button
         v-show="scope.row.reportStatus !=='2'"
            icon="el-icon-circle-check"
            type="text"
            size="small"
            plain
            @click="reportReview(scope.row)">审核</el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { getCompetitionList, removeCompetition, sureCompetition } from '@/api/crm/competition'
import { tableOption } from '@/const/crud/crm/reporting/competition'
import { mapGetters } from 'vuex'

export default {
  name: 'workreport',
  // components: {
  //   salesreportimport
  // },
  data() {
    return {
      //初始化坐标
        lnglat: {
          P: 29.563761,
          R: 106.55046400000003,
        },
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption
    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.crm_workreport_add, false),
        delBtn: this.vaildData(this.permissions.crm_workreport_del, false),
        editBtn: this.vaildData(this.permissions.crm_workreport_edit, false)
      }
    }
  },
  created() {
    this.getHeight()
  },
  methods: {
    getHeight() {
      const tableHeight = Math.max(window.innerHeight - 530, 200)
      this.tableOption.height = tableHeight + 'px'
    },
    async getList(page, params) {
      if (params && params.reportDate && params.reportDate.length > 1) {
        params['beginDate'] = params.reportDate[0]
        params['endDate'] = params.reportDate[1]
      }
      this.tableLoading = true
      const response = await getCompetitionList(Object.assign({
        current: page.currentPage,
        size: page.pageSize
      }, params))
      this.tableData = response.data.data.records
      this.page.total = response.data.data.total
      this.tableLoading = false
    },
    jumpToAdd() {
      this.$router.push({ path: '/reporting/addCompetition' })
    },
    jumpToEdit(row) {
      const query = {
        res: {
          reportSn:row.reportSn,
        }
      }
      this.$router.push({ path: '/reporting/editCompetition', query: query })
    },
    jumpToDetail(row) {
      console.log(row);
      
      const query = {
        res: {
          reportSn:row.reportSn,
        }
      }
      this.$router.push({ path: '/reporting/competitionDetail', query: query })
    },
    rowDel: function(row, index) {
      var _this = this
      this.$confirm('是否确认删除竞品上报单号' + row.reportSn, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        const params = {
          reportSn: row.reportSn
        }
        removeCompetition(params).then(response => {
          console.log(response);
          
          if (response.data.code === 0) {
            _this.$message({
              showClose: true,
              message: '删除成功',
              type: 'success'
            })
          }
          _this.getList(_this.page)
        })
      }).catch(function(err) {
      })
    },
    /**
       * @title 数据更新
       * @param row 为当前的数据
       * @param index 为当前更新数据的行数
       * @param done 为表单关闭函数
       *
       **/
    handleUpdate: function(row, index, done) {
      putObj(row).then(data => {
        this.tableData.splice(index, 1, Object.assign({}, row))
        this.$message({
          showClose: true,
          message: '修改成功',
          type: 'success'
        })
        done()
        this.getList(this.page)
      })
    },
    /**
       * @title 数据添加
       * @param row 为当前的数据
       * @param done 为表单关闭函数
       *
       **/
    handleSave: function(row, done) {
      addObj(row).then(data => {
        this.tableData.push(Object.assign({}, row))
        this.$message({
          showClose: true,
          message: '添加成功',
          type: 'success'
        })
        done()
        this.getList(this.page)
      })
    },
    /**
       * 搜索回调
       */
    searchChange(form, done) {
      this.getList(this.page, form)
      done()
    },
    /**
       * 刷新回调
       */
    refreshChange(info) {
      this.getList(this.page, this.filterForm(info.searchForm))
    },
    reportReview(row){
      console.log(row);
      
      let query = {
        res: {
          reportSn: row.reportSn
        }
      };
      this.$router.push({ path: "/reporting/competitionReview", query: query });
    },


    // reportReview(row) {
    //   const _this = this
    //   this.$confirm('是否确认竞品上报单号' + row.reportSn + '的上报数据', '提示', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(function() {
    //     const params = {
    //       reportSn: row.reportSn
    //     }
    //     sureCompetition(params).then(response => {
    //       if (response.data.code === 0 && response.data.data) {
    //         _this.$message({
    //           showClose: true,
    //           message: '确认成功',
    //           type: 'success'
    //         })
    //       }
    //       _this.getList(_this.page)
    //     })
    //   }).catch(function(err) {
    //   })
    // }
  }
}
</script>

<style lang="scss" scoped>
</style>
